package com.shanghaiyimu.yimiyangche.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shanghaiyimu.yimiyangche.R;
import com.shanghaiyimu.yimiyangche.adapter.MyFragmentViewPagerAdapter;
import com.shanghaiyimu.yimiyangche.fragment.AssessFragment;
import com.shanghaiyimu.yimiyangche.fragment.GoodsContentFragment;
import com.shanghaiyimu.yimiyangche.fragment.GoodsFragment;

import java.util.ArrayList;
import java.util.List;

import static android.view.View.INVISIBLE;

public class GoodsContentActivity extends AppCompatActivity implements View.OnClickListener{

    private TextView text1,text2,text3;
    private RelativeLayout linearLayout1,linearLayout2,linearLayout3;
    private ImageView imageView;
    private ViewPager mViewPager;
    private Fragment f1,f2,f3;
    private List<Fragment> listFragment = null;
    private MyFragmentViewPagerAdapter myFragmentViewPagerAdapter;
    private FragmentManager manager;
    private ImageView imageView1,imageView2,imageView3;
    private Button lijigoumai,jiarugouwuche;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_content);

        Intent intent = getIntent();
        String xx = intent.getExtras().getString("name");


        imageView = (ImageView) findViewById(R.id.goodscontent_bar_img_back);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        initView();
    }

    private void initView() {
        lijigoumai = (Button) findViewById(R.id.lijigoumai_btn_activity);
        jiarugouwuche = (Button) findViewById(R.id.jiarugouwuche_btn_activity);
        lijigoumai.setOnClickListener(this);
        jiarugouwuche.setOnClickListener(this);


        mViewPager = (ViewPager) findViewById(R.id.activity_goods_content_viewpager);
        listFragment = new ArrayList<Fragment>();

        f1 = new GoodsFragment();
        f2 = new GoodsContentFragment();
        f3 = new AssessFragment();
        listFragment.add(f1);
        listFragment.add(f2);
        listFragment.add(f3);

        manager = getSupportFragmentManager();
        myFragmentViewPagerAdapter  = new MyFragmentViewPagerAdapter(manager,listFragment);
        mViewPager.setAdapter(myFragmentViewPagerAdapter);
        mViewPager.setCurrentItem(0);

        text1 = (TextView) findViewById(R.id.id_goods_tv);
        text2 = (TextView) findViewById(R.id.id_content_tv);
        text3 = (TextView) findViewById(R.id.id_assess_tv);

        linearLayout1 = (RelativeLayout) findViewById(R.id.id_tab_goods_ll);
        linearLayout2 = (RelativeLayout) findViewById(R.id.id_tab_content_ll);
        linearLayout3 = (RelativeLayout) findViewById(R.id.id_tab_assess_ll);
        text1.setOnClickListener(this);
        text2.setOnClickListener(this);
        text3.setOnClickListener(this);
        linearLayout1.setOnClickListener(this);
        linearLayout2.setOnClickListener(this);
        linearLayout3.setOnClickListener(this);

        imageView1 = (ImageView) findViewById(R.id.id_goods_iv);
        imageView2 = (ImageView) findViewById(R.id.id_content_iv);
        imageView3 = (ImageView) findViewById(R.id.id_gassess_iv);

        imageView1.setVisibility(View.VISIBLE);
        linearLayout1.setClickable(false);
        text1.setClickable(false);
        text1.setTextColor(0xFFFA0111);

        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0){
                    text1.setTextColor(0xFFFA0111);
                    text2.setTextColor(0xFF000000);
                    text3.setTextColor(0xFF000000);


                    linearLayout1.setClickable(false);
                    text1.setClickable(false);
                    text2.setClickable(true);
                    linearLayout3.setClickable(true);
                    text3.setClickable(true);

                    imageView1.setVisibility(View.VISIBLE);
                    imageView2.setVisibility(INVISIBLE);
                    imageView3.setVisibility(INVISIBLE);
                }
                if (position == 1){
                    text2.setTextColor(0xFFFA0111);
                    text1.setTextColor(0xFF000000);
                    text3.setTextColor(0xFF000000);

                    linearLayout2.setClickable(false);
                    text2.setClickable(false);
                    linearLayout1.setClickable(true);
                    text1.setClickable(true);
                    linearLayout3.setClickable(true);
                    text3.setClickable(true);

                    imageView2.setVisibility(View.VISIBLE);
                    imageView1.setVisibility(INVISIBLE);
                    imageView3.setVisibility(INVISIBLE);
                }
                if (position == 2){
                    text3.setTextColor(0xFFFA0111);
                    text2.setTextColor(0xFF000000);
                    text1.setTextColor(0xFF000000);

                    linearLayout3.setClickable(false);
                    text3.setClickable(false);
                    linearLayout2.setClickable(true);
                    text2.setClickable(true);
                    linearLayout1.setClickable(true);
                    text1.setClickable(true);

                    imageView3.setVisibility(View.VISIBLE);
                    imageView2.setVisibility(INVISIBLE);
                    imageView1.setVisibility(INVISIBLE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.id_tab_goods_ll:
            case R.id.id_goods_tv:

                text1.setTextColor(0xFFFA0111);
                text2.setTextColor(0xFF000000);
                text3.setTextColor(0xFF000000);


                linearLayout1.setClickable(false);
                text1.setClickable(false);
                text2.setClickable(true);
                linearLayout3.setClickable(true);
                text3.setClickable(true);

                imageView1.setVisibility(View.VISIBLE);
                imageView2.setVisibility(INVISIBLE);
                imageView3.setVisibility(INVISIBLE);
                mViewPager.setCurrentItem(0);


                break;
            case R.id.id_tab_content_ll:
            case R.id.id_content_tv:

                text2.setTextColor(0xFFFA0111);
                text1.setTextColor(0xFF000000);
                text3.setTextColor(0xFF000000);

                mViewPager.setCurrentItem(1);
                linearLayout2.setClickable(false);
                text2.setClickable(false);
                linearLayout1.setClickable(true);
                text1.setClickable(true);
                linearLayout3.setClickable(true);
                text3.setClickable(true);

                imageView2.setVisibility(View.VISIBLE);
                imageView1.setVisibility(INVISIBLE);
                imageView3.setVisibility(INVISIBLE);

                break;
            case R.id.id_tab_assess_ll:
            case R.id.id_assess_tv:

                text3.setTextColor(0xFFFA0111);
                text2.setTextColor(0xFF000000);
                text1.setTextColor(0xFF000000);

                mViewPager.setCurrentItem(2);
                linearLayout3.setClickable(false);
                text3.setClickable(false);
                linearLayout2.setClickable(true);
                text2.setClickable(true);
                linearLayout1.setClickable(true);
                text1.setClickable(true);

                imageView3.setVisibility(View.VISIBLE);
                imageView2.setVisibility(INVISIBLE);
                imageView1.setVisibility(INVISIBLE);

                break;
            case R.id.jiarugouwuche_btn_activity:
                ((GoodsFragment) f1).showPop(0,1,0);

                break;
            case R.id.lijigoumai_btn_activity:

                ((GoodsFragment) f1).showPop(1,0,0);
                break;
        }
    }


}
